<template>
    <div class="minW1060-auto bg-fff mt10">
        <div class="pad20 ml10">

            <!--搜索条件-->
            <el-row class="text-left">
                    <div class="dlblock-mb20">
                        <span class="pr5 ml10">账号状态</span>
                        <el-select v-model="query.state" style="width: 150px" clearable>
                            <el-option label="全部" value=""></el-option>
                            <el-option label="正常" value="0"></el-option>
                            <el-option label="需要安全性验证" value="1"></el-option>
                            <el-option label="需要增强的安全性验证" value="2"></el-option>
                            <el-option label="锁定" value="3"></el-option>
                            <el-option label="禁用" value="4"></el-option>
                        </el-select>
                    </div>

                    <div class="dlblock-mb20">
                        <span class="pr5 ml10">角色名称</span>
                        <el-select v-model="query.roles" style="width: 150px" clearable>
                            <el-option label="全部" value=""></el-option>
                            <el-option :label="item.name" v-for="item in roles" :value="item.id"></el-option>
                        </el-select>
                    </div>

                    <div class="dlblock-mb20">
                        <span class="pr5 ml10">账号名称</span>
                        <el-input v-model.trim="query.loginId" size="medium" placeholder="账号名称" style="width: 200px;"
                                  :maxlength="10" show-word-limit clearable></el-input>
                    </div>

                    <div class="dlblock-mb20 pr5 pl5">
                        <el-button @click="query.page = 1,queryAdmin()" class="el-icon-search ml5" circle></el-button>
                    </div>
                    <div class="dlblock-mb20 pr5 pl5">
                        <!--<router-link :to="{path: '/account/edit',query:{menuName:'添加账号'}}">
                            <el-button type="primary">添加</el-button>
                        </router-link>-->
                        <el-button type="primary" @click="editBtn(1)">添加</el-button>
                    </div>

            </el-row>

            <!--表格-->
            <el-row>
                <el-table
                        class="my-table"
                        :data="adminList"
                        v-loading="loading"
                        :header-row-class-name="'my-table-header'"
                        ref="myTable"
                        border>
                    <el-table-column align="center" prop="loginId" label="账号名称"></el-table-column>
                    <el-table-column align="center" prop="speed" label="状态">
                        <template slot-scope="{row}">
                            <div v-if="row.state==0">正常</div>
                            <div v-else-if="row.state==1">需要安全性验证</div>
                            <div v-else-if="row.state==2">需要增强的安全性验证</div>
                            <div v-else-if="row.state==3">锁定</div>
                            <div v-else-if="row.state==4">禁用</div>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" prop="brithdayString" label="最后登录" width="300px">
                        <template slot-scope="{row}">
                            <p>{{row.lastLoginIP+"("+row.lastLoginIP2+")"}}</p>
                            <p v-text="row.lastLoginTime"></p>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" prop="brithdayString" label="客户端">
                        <template slot-scope="{row}">
                            <p>{{row.browser}}</p>
                            <p>{{row.system}}</p>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" prop="creationTime" label="创建时间"></el-table-column>

                    <el-table-column align="center" prop="lockedTime" label="最后锁定时间"></el-table-column>

                    <el-table-column align="center" label="操作" fixed="right">
                        <template slot-scope="{row}">
                            <!--<router-link :to="{path:'/account/edit',query:{admin:row,menuName:'账号编辑'}}">
                                <el-button type="text">编辑</el-button>
                            </router-link>&emsp;-->
                            <el-button type="text" @click="editBtn(2,row)">编辑</el-button>
                            <el-button type="text" style="color: #f56c6c" @click="deleteOne(row.id)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-row>
            <br>

            <!--分页控件-->
            <el-row :gutter="20">
                <el-col :span="12" :offset="6" align="center">
                    <el-pagination background layout="total, prev, pager, next" :total="query.total"
                                   :page-size="query.size"
                                   :current-page="query.page" @current-change="pageChange">
                    </el-pagination>
                </el-col>
            </el-row>

        </div>

        <el-dialog :title="`编辑角色-${editData.add ==1 ? '添加' :'编辑'}`" :visible.sync="editData.modal" width="1000px" :close-on-click-modal="false" v-dialogDrag>
            <div class="pad20 ml10">
                <el-row>
                    <el-col align="left">
                        <h1>基本信息</h1>
                    </el-col>
                </el-row>

                <el-row :gutter="30" class="mt20">
                    <el-form label-position="left" :rules="rules" label-width="110px" :model="admin"
                             ref="admin">
                        <el-row>
                            <el-col :span="10" :offset="1">
                                <el-form-item label="账号名称" prop="loginId">
                                    <el-input v-model.trim="admin.loginId" :maxlength="20" show-word-limit></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="10" :offset="1">
                                <el-form-item label="账号密码" prop="loginPwd">
                                    <el-input v-model.trim="admin.loginPwd" type="password" @focus="checkPwd" :maxlength="20" show-word-limit></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="10" :offset="1">
                                <el-form-item label="友好名称" prop="nickName">
                                    <el-input v-model.trim="admin.nickName" :maxlength="20" show-word-limit></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="10" :offset="1">
                                <el-form-item label="性别">
                                    <el-select v-model="admin.gender" clearable style="width: 100%">
                                        <el-option label="未知（保密）" :value="0"></el-option>
                                        <el-option label="男" :value="1"></el-option>
                                        <el-option label="女" :value="2"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row v-if="show">
                            <el-col :span="15" :offset="1">
                                <el-form-item label="账号状态" prop="state" label-width="110px">
                                    <el-radio-group v-model="admin.state">
                                        <el-radio :label="0">启用账号</el-radio>
                                        <el-radio :label="3">锁定账号</el-radio>
                                        <el-radio :label="4">禁用账号</el-radio>
                                    </el-radio-group>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </el-row>
                <el-row class="mt10" v-if="show">
                    <el-col align="left">
                        <h1>账号登录日志</h1>
                    </el-col>
                </el-row>
                <el-row class="mt20" v-if="show">
                    <el-table
                            height="260"
                            class="my-table"
                            :data="adminLogsList"
                            v-loading="loading"
                            :header-row-class-name="'my-table-header'"
                            ref="myTable"
                            border>
                        <el-table-column type="index" align="center" width="80%" label="序号"></el-table-column>
                        <el-table-column align="center" prop="loginId" label="账号名称"></el-table-column>
                        <el-table-column align="center" prop="speed" label="登录状态">
                            <template slot-scope="{row}">
                                <div v-if="row.state==0">登录成功</div>
                                <div v-else-if="row.state==1">用户名不存在</div>
                                <div v-else-if="row.state==2">密码错误</div>
                                <div v-else-if="row.state==3">非法客户端</div>
                                <div v-else-if="row.state==4">禁止登录</div>
                            </template>
                        </el-table-column>

                        <el-table-column align="center" prop="brithdayString" label="IP">
                            <template slot-scope="{row}">
                                <p>{{row.ip}}</p>
                            </template>
                        </el-table-column>

                        <el-table-column align="center" prop="brithdayString" label="客户端">
                            <template slot-scope="{row}">
                                <p>{{row.browser}}</p>
                                <p>{{row.system}}</p>
                            </template>
                        </el-table-column>
                        <el-table-column align="center" prop="time" label="账号登录时间"></el-table-column>
                    </el-table>
                </el-row>
                <!--分页控件-->
                <el-row :gutter="20" v-if="show" class="mt20">
                    <el-col :span="12" :offset="6" align="center">
                        <el-pagination background layout="total, prev, pager, next" :total="admin.total"
                                       :page-size="admin.size"
                                       :current-page="admin.page" @current-change="pageChanges">
                        </el-pagination>
                    </el-col>
                </el-row>
                <el-row>

                    <el-col align="center">
                        <div class="dlblock-mb20 pr10 ml20">
                            <el-button type="primary" @click="submitForm('admin')">保存</el-button>
                        </div>
                        <div class="dlblock-mb20 pr10">
                            <el-button type="danger" @click="resetForm('admin')">重置</el-button>
                        </div>
                    </el-col>

                </el-row>
            </div>
        </el-dialog>
    </div>
</template>

<script>

    import {getAdminInfo, deleteAdmin, exportAdminInfo, deleteAdminAll, getRolesInFo,savaAdmin, getAdminLogsInfo} from '@/api';
    export default {
        name: "index1",
        components: {},
        data() {
            const VerificationPwd = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'))
                } else if (value.length < 6) {
                    callback(new Error('密码不少于6位'))
                } else {
                    callback()
                }
            }
            return {

                //加载动画
                loading: false,

                //账号列表
                adminList: [],

                //账号查询参数
                query: {
                    loginId: '',
                    state: '',
                    roles: '',
                    page: 1,
                    size: 15,

                },
                admin: {
                    loginId: '',
                    loginPwd: '',
                    nickName: '',
                    gender: '',
                    state: '',
                    disabled: '',
                    id: '',
                    page: 1,
                    size: 10,
                    total: 0
                },
                show: false,
                //角色
                roles: [],
                param: {
                    visible: 0,
                    size: 100000,
                },

                editData:{
                    modal:false,
                    add:1

                },
                adminLogsList:[],
                //全选id
                adminIds: [],
                //复选框 序号
                number: [],
                checked: false,
                //验证规则
                rules: {
                    loginId: [
                        {required: true, message: '请输入账号', trigger: 'blur'},
                    ],
                    loginPwd: [
                        {required: true, validator: VerificationPwd, trigger: 'blur'},
                    ],
                    disabled: [
                        {required: true, message: '请选择账号状态', trigger: 'blur'},
                    ],
                    state: [
                        {required: true, message: '请选择锁定状态', trigger: 'blur'},
                    ],
                },
            }
        },
        watch: {
            number() {
                if (this.number.length == 10) {
                    this.checked = true;
                } else {
                    this.checked = false;
                }
            }
        },
        methods: {
            checkPwd() {
                if (this.pastPwd == this.admin.loginPwd) {
                    this.admin.loginPwd = ''
                }
            },

            editBtn(t,row){
               if (t==1){
                   this.editData.modal=true
                   this.editData.add=1
                   this.admin.loginId=''
                   this.admin.loginPwd=''
                   this.admin.nickName=''
                   this.admin.gender=''
                   this.admin.state=''
                   this.adminLogsList=[]
                   this.show=false
               }else {
                   this.editData.modal=true
                   this.editData.add=2
                   this.admin = row
                   this.pastPwd = row.loginPwd
                   this.show = true
                   this.queryAdminLogs()
               }
           },

            //提交表单
            submitForm(formName) {

                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.pastPwd == this.admin.loginPwd) {
                            this.admin.loginPwd = null
                        }
                        savaAdmin(this.admin).then(res => {
                            if (res.code == 200) {
                                this.$message.success("保存成功");
                                this.editData.modal=false
                                this.queryAdmin()
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            //查询账号登录日志
            queryAdminLogs() {
                this.loading = true
                getAdminLogsInfo(this.admin).then(res => {
                    if (res != null && res.code == 200) {
                        this.adminLogsList = res.data.list;
                        this.admin.total = res.data.count
                    } else {
                        this.$message.info(res.message)
                    }
                }).finally(
                    this.loading = false
                )
            },
            // 分页查询
            pageChanges(val) {
                this.admin.page = val
                this.queryAdminLogs()
            },
            //重置
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            //复选框加序号
            checkDetail(val) {
                if (event.target.checked == true) {
                    this.adminIds.push(val.id)
                } else {
                    this.adminIds.splice(this.adminIds.indexOf(val.id), 1);
                }
            },
            checkAll() {
                this.checked = !this.checked
                if (this.checked == true) {
                    this.checked = !this.checked
                    this.adminIds = []
                    this.number = [];
                } else {
                    this.adminList.forEach((item) => {
                        this.number.push(item.id)
                        this.adminIds.push(item.id)
                    })
                }
            },

            //查询角色
            getRolesFn() {
                getRolesInFo(this.param).then(res => {
                    this.roles = res.data.list
                })
            },
            //查询账号
            queryAdmin() {
                this.loading = true
                getAdminInfo(this.query).then(res => {
                    if (res != null && res.code == 200) {
                        this.adminList = res.data.list;
                        this.query.total = res.data.count
                    } else {
                        this.$message.info(res.message)
                    }
                }).finally(
                    this.loading = false
                )
            },

            //删除方法
            deleteOne(data) {
                deleteAdmin({id: data}).then(res => {
                    if (res.code == 200) {
                        this.$message.success("删除成功！！！")
                        this.queryAdmin()
                    } else {
                        this.$message.info("删除失败！！！")
                    }
                })
            },

            //导出
            exportAdmin() {
                exportAdminInfo({
                    state: this.state,
                    roles: this.roles,
                    loginId: this.loginId
                })
            },

            //删除全部
            deleteAll() {
                if (this.adminIds.length != 0) {
                    deleteAdminAll({
                        adminIds: this.adminIds
                    }).then(res => {
                        console.log(res)
                        if (res != null && res.code == 200) {
                            this.$message.success("删除成功！！！")
                            this.queryAdmin()
                        } else {
                            this.$message.info("删除失败！！！")
                        }
                    })
                }
            },
            // 分页查询
            pageChange(val) {
                this.query.page = val
                this.queryAdmin()
            },
        },
        mounted() {
            this.getRolesFn()
        },
        created() {
            this.queryAdmin()
        }
    }
</script>

<style scoped>
    span.checkbox {
        position: relative;
        left: 5px;
        top: -1px;
    }
    .my-table /deep/ tr, .my-table /deep/ td{
        padding: 0 !important;
        height: 50px;
        line-height: 50px;
    }

</style>
